home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / kode ƒ / kant load-save.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  3KB  |  136 lines

  1. #include <Script.h>
  2. #include "error.h"
  3. #include "kant load-save.h"
  4. #include "kant main window.h"
  5. #include "kant.h"
  6. #include "file interface.h"
  7. #include "text twiddling.h"
  8. #include "dialogs.h"
  9. #include "environment.h"
  10. #include "util.h"
  11. #include "graphics.h"
  12. #include "window layer.h"
  13. #include "program globals.h"
  14.  
  15. void LoadSaveDispatch(Boolean isLoad, Boolean useOldFile)
  16. {
  17.     FSSpec            saveFile;
  18.     Boolean            alreadyExists;
  19.     enum ErrorTypes    theError;
  20.     WindowPtr        mainWindowPtr;
  21.     
  22.     if ((!isLoad) && ((mainWindowPtr=GetIndWindowPtr(kMainWindow))==0L))
  23.         return;
  24.     
  25.     if (isLoad)
  26.     {
  27.         if (GetSourceFile(&saveFile, SAVE_TYPE))
  28.         {
  29.             theError=GetTheFile(&saveFile);
  30.             if (theError!=noErr)
  31.                 HandleError(theError, FALSE, FALSE);
  32.         }
  33.     }
  34.     else
  35.     {
  36.         saveFile=GetWindowFS(mainWindowPtr);
  37.         useOldFile=useOldFile&&(saveFile.name[0]!=0x00);
  38.         if (!useOldFile)
  39.         {
  40.             if (!GetDestFile(&saveFile, &alreadyExists, "\pSave document as..."))
  41.                 return;
  42.         }
  43.         
  44.         theError=SaveTheFile(saveFile, alreadyExists);
  45.         if (theError!=noErr)
  46.             HandleError(theError, FALSE, FALSE);
  47.     }
  48. }
  49.  
  50. enum ErrorTypes SaveTheFile(FSSpec saveFile, Boolean alreadyExists)
  51. {
  52.     OSErr            theError;
  53.     short            thisFile;
  54.     long            count;
  55.     TEHandle        hTE;
  56.     WindowPtr        theWindow;
  57.     
  58.     theError=noErr;
  59.     
  60.     if (!alreadyExists)
  61.     {
  62.         theError=FSpCreate(&saveFile, CREATOR, SAVE_TYPE, smSystemScript);
  63.         FlushVol(0L, saveFile.vRefNum);
  64.     }
  65.     
  66.     if (theError!=noErr)
  67.         return kCantCreateFile;
  68.     
  69.     FSpCreate(&saveFile, CREATOR, SAVE_TYPE, smSystemScript);
  70.     theError=FSpOpenDF(&saveFile, fsRdWrPerm, &thisFile);
  71.     
  72.     if (theError!=noErr)
  73.         return kCantOpenFileToSave;
  74.  
  75.     theWindow=GetIndWindowPtr(kMainWindow);
  76.     hTE=GetWindowTE(theWindow);
  77.     count=(**hTE).teLength;
  78.     theError=SetEOF(thisFile, count);
  79.     if (theError!=noErr)
  80.     {
  81.         FSClose(thisFile);
  82.         return kDiskFull;
  83.     }
  84.     
  85.     SetFPos(thisFile, 1, 0L);
  86.     theError=FSWrite(thisFile, &count, *((**hTE).hText));
  87.     
  88.     FSClose(thisFile);
  89.     FlushVol(0L, saveFile.vRefNum);
  90.  
  91.     if (theError!=noErr)
  92.     {
  93.         saveFile.name[0]=0x00;
  94.         return kCantWriteFile;
  95.     }
  96.     
  97.     SetWTitle(theWindow, saveFile.name);
  98.     SetWindowTitle(theWindow, saveFile.name);
  99.     SetWindowFS(theWindow, saveFile);
  100.     SetWindowIsModified(theWindow, FALSE);
  101.     
  102.     return allsWell;
  103. }
  104.  
  105. enum ErrorTypes GetTheFile(FSSpec *saveFile)
  106. {
  107.     short            thisFile;
  108.     long            count;
  109.     OSErr            theError;
  110.     Ptr                data;
  111.     WindowPtr        theWindow;
  112.     
  113.     theError=FSpOpenDF(saveFile, fsRdPerm, &thisFile);
  114.     if (theError!=noErr)
  115.         return kCantOpenFileToLoad;
  116.     
  117.     GetEOF(thisFile, &count);
  118.     data=NewPtr(count);
  119.     SetFPos(thisFile, 1, 0L);
  120.     theError=FSRead(thisFile, &count, data);
  121.     FSClose(thisFile);
  122.     
  123.     if (theError!=noErr)
  124.         return kCantLoadFile;
  125.     
  126.     SetMainWindowTitle(saveFile->name);
  127.     OpenTheIndWindow(kMainWindow);
  128.     theWindow=GetIndWindowPtr(kMainWindow);
  129.     SetTheText(theWindow, data, count);
  130.     SetWindowFS(theWindow, *saveFile);
  131.     SetWindowIsModified(theWindow, FALSE);
  132.     DisposePtr(data);
  133.     
  134.     return allsWell;
  135. }
  136.